//
//  ZZCLawView.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/22.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCLawView.h"

@implementation ZZCLawView

+ (instancetype)lawView {
    
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
    return [nib instantiateWithOwner:nil options:nil].lastObject;
}
-(void)layoutSubviews {
    self.phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.cardTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.mailboxTextField.keyboardType = UIKeyboardTypeEmailAddress;
    
}

//- (void)layoutSubviews {
//    [self.phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//    self.phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    //
//    [self.cardTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//    self.cardTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    //
//    [self.nameTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//    self.nameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    //
//    [self.mailboxTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//    self.mailboxTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

//}
//-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    if (textField == self.phoneTextField){
//        NSUInteger lengthOfString = string.length;
//        for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {//只允许数字输入
//            unichar character = [string characterAtIndex:loopIndex];
//            if (character < 48) return NO; // 48 unichar for 0
//            if (character > 57) return NO; // 57 unichar for 9
//        }
//    }
//    return YES;
//}      // Check for total length
//        NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
//        if (proposedNewLength > 11) return NO;//限制长度
//        return YES;
//    }else{
//        NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
//        if ( proposedNewLength > 20) return NO;//限制长度
//        return YES;
//    }
//}

//- (void)textFieldDidChange:(UITextField *)textField{
//    UITextRange *selectedRange = textField.markedTextRange;
//    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
//    if (!position) {
//        // 没有高亮选择的字
//        // 1. 过滤非汉字、字母、数字字符
//        self.phoneTextField.text = [self filterCharactor:textField.text withRegex:@"[^a-zA-Z0-9\u4e00-\u9fa5]"];
//        self.cardTextField.text = [self filterCharactor:textField.text withRegex:@"[^a-zA-Z0-9\u4e00-\u9fa5]"];
//        self.mailboxTextField.text = [self filterCharactor:textField.text withRegex:@"[^a-zA-Z0-9\u4e00-\u9fa5]"];
//        self.nameTextField.text = [self filterCharactor:textField.text withRegex:@"[^a-zA-Z0-9\u4e00-\u9fa5]"];

        // 2. 截取
//        if (self.phoneTextField.text.length >= 11) {
//            self.phoneTextField.text = [self.phoneTextField.text substringToIndex:11];
//        }
//        else if (self.cardTextField.text.length >= 18) {
//            self.cardTextField.text = [self.phoneTextField.text substringToIndex:18];
//        }
//
//        else if (self.mailboxTextField.text.length >= 11) {
//            self.mailboxTextField.text = [self.mailboxTextField.text substringToIndex:11];
//        }
//
//        else if (self.nameTextField.text.length >= 11) {
//            self.nameTextField.text = [self.nameTextField.text substringToIndex:11];
//        }

//    } else {
//        // 有高亮选择的字 不做任何操作
//    }
//}
//// 过滤字符串中的非汉字、字母、数字
//- (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{
//    NSString *filterText = string;
//    NSError *error = NULL;
//    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
//    NSString *result = [regex stringByReplacingMatchesInString:filterText options:NSMatchingReportCompletion range:NSMakeRange(0, filterText.length) withTemplate:@""];
//    return result;
//}
//// return键按下
//- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//    [self endEditing:YES];
//    if (textField.text.length < 2) {
//        // 提示用户，名称长度至少为2位
//        return NO;
//    }
//    // 设置用户名
//    return YES;
//}
//
@end
